package thread;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class Demo16 {
    public static void main(String[] args) {

        BlockingDeque<Integer> queue = new LinkedBlockingDeque();
        // 消费者
        Thread t1 = new Thread(()->{
            while (true){
                try {
                    int value = queue.take();
                    System.out.println("消费元素：" + value);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();

        // 生产者
        Thread t2 = new Thread(()->{
            int value = 0;
            while (true){
                System.out.println("生产元素" + value);
                try {
                    queue.put(value);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                value++;
            }
        });
        t2.start();

        // 上述代码，让生产者，每隔 1s 生产一个元素
        // 让消费者直接消费，不受限制

    }
}
